<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JSON 在线编辑器</title>
    <link rel="stylesheet" href="styles.css">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
    <link href="https://unpkg.com/vanilla-jsoneditor@0.23.8/themes/jse-theme-dark.css" rel="stylesheet">
    <link href="https://unpkg.com/vanilla-jsoneditor@0.23.8/dist/vanilla-jsoneditor.css" rel="stylesheet">
    <style>
        .editor-container {
            display: flex;
            flex-direction: column;
            gap: 20px;
            margin-top: 20px;
        }
        
        .editor-wrapper {
            background: var(--card-background);
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .editor-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }
        
        .editor-header h2 {
            margin: 0;
            color: var(--text-color);
            font-size: 18px;
        }
        
        .editor-controls {
            display: flex;
            gap: 10px;
            align-items: center;
        }
        
        .mode-selector {
            display: flex;
            background: var(--background-color);
            border-radius: 6px;
            padding: 2px;
            border: 1px solid var(--border-color);
        }
        
        .mode-btn {
            padding: 6px 12px;
            border: none;
            background: transparent;
            color: var(--text-color);
            cursor: pointer;
            border-radius: 4px;
            font-size: 12px;
            transition: all 0.2s;
        }
        
        .mode-btn.active {
            background: var(--primary-color);
            color: white;
        }
        
        .action-btn {
            padding: 8px 16px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.2s;
            display: flex;
            align-items: center;
            gap: 6px;
        }
        
        .action-btn.primary {
            background: var(--primary-color);
            color: white;
        }
        
        .action-btn.secondary {
            background: var(--secondary-color);
            color: white;
        }
        
        .action-btn:hover {
            opacity: 0.9;
            transform: translateY(-1px);
        }
        
        #jsoneditor {
            height: 500px;
            border: 1px solid var(--border-color);
            border-radius: 6px;
        }
        
        .status-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 10px;
            padding: 8px 12px;
            background: var(--background-color);
            border-radius: 4px;
            font-size: 12px;
            color: var(--text-color-secondary);
        }
        
        .status-info {
            display: flex;
            gap: 15px;
        }
        
        .error-message {
            color: var(--error-color);
            background: rgba(220, 53, 69, 0.1);
            padding: 10px;
            border-radius: 4px;
            margin-top: 10px;
            display: none;
        }
        
        .success-message {
            color: var(--success-color);
            background: rgba(40, 167, 69, 0.1);
            padding: 10px;
            border-radius: 4px;
            margin-top: 10px;
            display: none;
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .editor-header {
                flex-direction: column;
                gap: 10px;
                align-items: stretch;
            }
            
            .editor-controls {
                justify-content: center;
                flex-wrap: wrap;
            }
            
            #jsoneditor {
                height: 400px;
            }
        }
    </style>
    <script type="text/javascript" charset="UTF-8" src="https://cdn.wwads.cn/js/makemoney.js" async></script>
</head>
<body>
    <header>
        <div class="header-container">
            <div class="header-left">
                <div class="logo">
                    <span><a href="https://geekdaxue.co/" target="_blank"><img src="https://geekdaxue.co/static/images/logo_platypus.png" height="30px"></a></span>
                </div>
                <nav class="main-navigation">
                    <div class="nav-item dropdown">
                        <a href="#" class="nav-link">
                            JSON工具 <i class="fas fa-chevron-down"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a href="../json/index.html">JSON 格式化</a>
                            <a href="../json/parser.html">JSON 格式校验</a>
                            <a href="../json/excel.html">JSON 转 Excel/CSV</a>
                            <a href="../json/jsonxml.html">JSON 与 XML 转换</a>
                            <a href="../json/editor.html">JSON 编辑器</a>
                            <a href="../json/txt2json_arr.html">文本转JSON数组</a>
                        </div>
                    </div>
                    <div class="nav-item dropdown">
                        <a href="#" class="nav-link">
                            编码转换 <i class="fas fa-chevron-down"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a href="../coding/base64.html">Base64 编码</a>
                            <a href="../coding/urlencoding.html">URL 编码</a>
                            <a href="../coding/md5.html">MD5 加密</a>
                            <a href="../coding/unicode.html">Unicode 转换</a>
                            <a href="../coding/hex_convert.html">进制转换</a>
                        </div>
                    </div>
                    <div class="nav-item dropdown">
                        <a href="#" class="nav-link">
                            文本工具 <i class="fas fa-chevron-down"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a href="../txt/diff.html">文本对比</a>
                            <a href="../txt/replace.html">文本替换</a>
                            <a href="../txt/remove.html">空白行移除</a>
                            <a href="../txt/cn_trans.html">繁简体转换</a>
                        </div>
                    </div>
                    <div class="nav-item dropdown">
                        <a href="#" class="nav-link">
                            图片工具 <i class="fas fa-chevron-down"></i>
                        </a>
                        <div class="dropdown-menu">
                            <a href="../img/2svg.html">图片转 SVG</a>
                            <a href="../img/qr_code.html">二维码生成</a>
                            <a href="../img/barcode.html">条形码生成</a>
                        </div>
                    </div>
                </nav>
            </div>
            <div class="header-controls">
                <div class="theme-toggle">
                    <input type="checkbox" id="theme-switch" class="theme-switch">
                    <label for="theme-switch" class="theme-switch-label">
                        <i class="fas fa-sun"></i>
                        <i class="fas fa-moon"></i>
                        <span class="slider"></span>
                    </label>
                </div>
                <div class="share-button" id="share-button" title="分享给朋友">
                    <i class="fas fa-share-alt"></i>
                    <span>分享</span>
                </div>
            </div>
        </div>
    </header>

    <div class="container">
        <h1>JSON 编辑器</h1>
        <div id="privacy-notice" class="privacy-notice">
            <div class="privacy-notice-content">
                <i class="fas fa-shield-alt"></i>
                <p>安全提示：您的数据仅在本地浏览器中处理，不会上传到服务器，请放心使用！</p>
                <button id="close-notice" class="close-notice" title="关闭提示">
                    <i class="fas fa-times"></i>
                </button>
            </div>
        </div>
        
        <main>
            <div class="editor-container">
                <div class="editor-wrapper">
                    <div class="editor-header">
                        <h2>JSON 编辑器</h2>
                        <div class="editor-controls">
                            <div class="mode-selector">
                                <button class="mode-btn active" data-mode="tree">树形视图</button>
                                <button class="mode-btn" data-mode="code">代码视图</button>
                                <button class="mode-btn" data-mode="text">文本视图</button>
                            </div>
                            <button class="action-btn primary" id="format-btn">
                                <i class="fas fa-magic"></i> 格式化
                            </button>
                            <button class="action-btn secondary" id="validate-btn">
                                <i class="fas fa-check"></i> 验证
                            </button>
                            <button class="action-btn secondary" id="copy-btn">
                                <i class="fas fa-copy"></i> 复制
                            </button>
                            <button class="action-btn secondary" id="clear-btn">
                                <i class="fas fa-trash"></i> 清空
                            </button>
                        </div>
                    </div>
                    
                    <div id="jsoneditor"></div>
                    
                    <div class="status-bar">
                        <div class="status-info">
                            <span id="status-mode">模式: 树形视图</span>
                            <span id="status-size">大小: 0 字符</span>
                            <span id="status-lines">行数: 0</span>
                        </div>
                        <div id="status-validation">JSON 有效</div>
                    </div>
                    
                    <div id="error-message" class="error-message"></div>
                    <div id="success-message" class="success-message"></div>
                </div>
            </div>
        </main>
        <div class="wwads-cn wwads-horizontal" data-id="321" style="max-width:350px"></div>
    </div>
    <footer>
            <div class="footer-container">
                <div class="footer-main">
                    <div class="footer-section about-us">
                        <h3>关于我们</h3>
                        <p>我们提供一系列高效、便捷的在线工具，帮助开发者、非技术人员提高工作效率，解决日常开发中遇到的各种问题。</p>
                        <p>Build With <i class="fa-solid fa-heart" style="color: red;"></i> For SF.</p>
                    </div>
                    <div class="footer-section links">
                        <h3>工具导航</h3>
                        <div class="links-columns">
                            <ul>
                                <li><a href="../json/index.html">JSON 工具</a></li>
                                <li><a href="../img/index.html">图片处理</a></li>
                                <li><a href="#">开发工具</a></li>
                            </ul>
                            <ul>
                                <li><a href="../coding/index.html">编码转换</a></li>
                                <li><a href="../txt/index.html">文本工具</a></li>
                            </ul>
                        </div>
                    </div>
                    <div class="footer-section contact">
                        <h3>联系我们</h3>
                        <div class="social-icons">
                            <a href="#" title="GitHub"><i class="fab fa-github"></i></a>
                            <a href="#" title="Twitter"><i class="fab fa-twitter"></i></a>
                            <a href="#" title="WeChat"><i class="fab fa-weixin"></i></a>
                            <a href="mailto:example@example.com" title="Email"><i class="fas fa-envelope"></i></a>
                        </div>
                        <p>如有问题或建议，请随时联系我们</p>
                    </div>
                </div>
                <div class="footer-bottom">
                    <p>© 2025 JSON 格式化工具 Pro. 保留所有权利</p>
                    <div class="footer-legal">
                        <a href="#">使用条款</a>
                        <a href="#">隐私政策</a>
                    </div>
                </div>
            </div>
        </footer>
    
    <div id="copy-notification" class="copy-notification">已复制到剪贴板！</div>
    <div id="share-notification" class="copy-notification">已复制网址，赶快让更多的小伙伴知道我吧！</div>
    
    <!-- 引入 vanilla-jsoneditor CSS -->
    <link href="https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.23.8/themes/jse-theme-dark.css" rel="stylesheet" type="text/css">
    <link href="https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.23.8/dist/vanilla-jsoneditor.css" rel="stylesheet" type="text/css">
    
    <script type="module">
        import { createJSONEditor } from 'https://cdn.jsdelivr.net/npm/vanilla-jsoneditor/standalone.js';

        document.addEventListener('DOMContentLoaded', function() {
            // 获取DOM元素
            const themeSwitch = document.getElementById('theme-switch');
            const privacyNotice = document.getElementById('privacy-notice');
            const closeNoticeBtn = document.getElementById('close-notice');
            const shareButton = document.getElementById('share-button');
            const copyNotification = document.getElementById('copy-notification');
            const shareNotification = document.getElementById('share-notification');
            const errorMessage = document.getElementById('error-message');
            const successMessage = document.getElementById('success-message');
            
            // 编辑器相关元素
            const modeButtons = document.querySelectorAll('.mode-btn');
            const formatBtn = document.getElementById('format-btn');
            const validateBtn = document.getElementById('validate-btn');
            const copyBtn = document.getElementById('copy-btn');
            const clearBtn = document.getElementById('clear-btn');
            const statusMode = document.getElementById('status-mode');
            const statusSize = document.getElementById('status-size');
            const statusLines = document.getElementById('status-lines');
            const statusValidation = document.getElementById('status-validation');
            
            // 初始化编辑器
            let editor;
            let currentMode = 'tree';
            
            // 检查本地存储中的主题设置
            if (localStorage.getItem('theme') === 'dark') {
                document.documentElement.setAttribute('data-theme', 'dark');
                themeSwitch.checked = true;
            }
            
            // 主题切换功能
            themeSwitch.addEventListener('change', function() {
                if (this.checked) {
                    document.documentElement.setAttribute('data-theme', 'dark');
                    localStorage.setItem('theme', 'dark');
                } else {
                    document.documentElement.removeAttribute('data-theme');
                    localStorage.setItem('theme', 'light');
                }
                
                // 重新初始化编辑器以应用主题
                if (editor) {
                    const content = editor.get();
                    initEditor();
                    editor.set(content);
                }
            });
            
            // 隐私提示框关闭功能
            closeNoticeBtn.addEventListener('click', function() {
                privacyNotice.style.display = 'none';
                localStorage.setItem('privacyNoticeClosed', 'true');
            });
            
            // 检查是否需要显示隐私提示
            if (localStorage.getItem('privacyNoticeClosed') === 'true') {
                privacyNotice.style.display = 'none';
            }
            
            // 分享功能
            shareButton.addEventListener('click', function() {
                const url = window.location.href;
                navigator.clipboard.writeText(url).then(function() {
                    showNotification(shareNotification);
                }).catch(function() {
                    console.error('复制失败');
                });
            });
            
            // 显示通知
            function showNotification(notification) {
                notification.style.display = 'block';
                notification.style.opacity = '1';
                setTimeout(function() {
                    notification.style.opacity = '0';
                    setTimeout(function() {
                        notification.style.display = 'none';
                    }, 300);
                }, 2000);
            }
            
            // 显示错误消息
            function showError(message) {
                errorMessage.textContent = message;
                errorMessage.style.display = 'block';
                successMessage.style.display = 'none';
                setTimeout(() => {
                    errorMessage.style.display = 'none';
                }, 5000);
            }
            
            // 显示成功消息
            function showSuccess(message) {
                successMessage.textContent = message;
                successMessage.style.display = 'block';
                errorMessage.style.display = 'none';
                setTimeout(() => {
                    successMessage.style.display = 'none';
                }, 3000);
            }
            
            // 初始化编辑器
            function initEditor() {
                if (editor) {
                    editor.destroy();
                }
                
                const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
                
                editor = createJSONEditor({
                    target: document.getElementById('jsoneditor'),
                    props: {
                        mode: currentMode,
                        mainMenuBar: false,
                        navigationBar: false,
                        statusBar: false,
                        askToFormat: false,
                        readOnly: false,
                        indentation: 2,
                        tabSize: 2,
                        content: {
                            json: {
                                "name": "示例",
                                "version": "1.0.0",
                                "description": "这是一个JSON编辑器示例",
                                "features": [
                                    "语法高亮",
                                    "格式化",
                                    "验证",
                                    "多种视图模式"
                                ],
                                "config": {
                                    "theme": "auto",
                                    "autoSave": true
                                }
                            }
                        },
                        onChange: (content, previousContent, { contentErrors, patchResult }) => {
                            updateStatus();
                            if (contentErrors && contentErrors.length > 0) {
                                statusValidation.textContent = `错误: ${contentErrors.length} 个`;
                                statusValidation.style.color = 'var(--error-color)';
                            } else {
                                statusValidation.textContent = 'JSON 有效';
                                statusValidation.style.color = 'var(--success-color)';
                            }
                        }
                    }
                });
                
                updateStatus();
            }
            
            // 更新状态栏
            function updateStatus() {
                try {
                    const content = editor.get();
                    const text = content.text || JSON.stringify(content.json, null, 2);
                    const lines = text.split('\n').length;
                    const size = text.length;
                    
                    statusSize.textContent = `大小: ${size} 字符`;
                    statusLines.textContent = `行数: ${lines}`;
                } catch (error) {
                    // 忽略错误
                }
            }
            
            // 模式切换
            modeButtons.forEach(btn => {
                btn.addEventListener('click', function() {
                    const mode = this.dataset.mode;
                    if (mode === currentMode) return;
                    
                    // 更新按钮状态
                    modeButtons.forEach(b => b.classList.remove('active'));
                    this.classList.add('active');
                    
                    // 保存当前内容
                    const content = editor.get();
                    
                    // 更新模式
                    currentMode = mode;
                    
                    // 重新初始化编辑器
                    initEditor();
                    
                    // 恢复内容
                    try {
                        editor.set(content);
                    } catch (error) {
                        console.warn('切换模式时恢复内容失败:', error);
                    }
                    
                    // 更新状态
                    const modeNames = {
                        'tree': '树形视图',
                        'code': '代码视图',
                        'text': '文本视图'
                    };
                    statusMode.textContent = `模式: ${modeNames[mode]}`;
                });
            });
            
            // 格式化按钮
            formatBtn.addEventListener('click', function() {
                try {
                    const content = editor.get();
                    if (content.json) {
                        editor.set({
                            json: content.json
                        });
                        showSuccess('JSON 格式化成功！');
                    } else if (content.text) {
                        const parsed = JSON.parse(content.text);
                        editor.set({
                            json: parsed
                        });
                        showSuccess('JSON 格式化成功！');
                    }
                } catch (error) {
                    showError('格式化失败: ' + error.message);
                }
            });
            
            // 验证按钮
            validateBtn.addEventListener('click', function() {
                try {
                    const content = editor.get();
                    if (content.json) {
                        showSuccess('JSON 格式正确！');
                    } else if (content.text) {
                        JSON.parse(content.text);
                        showSuccess('JSON 格式正确！');
                    }
                } catch (error) {
                    showError('JSON 格式错误: ' + error.message);
                }
            });
            
            // 复制按钮
            copyBtn.addEventListener('click', function() {
                try {
                    const content = editor.get();
                    const text = content.text || JSON.stringify(content.json, null, 2);
                    
                    navigator.clipboard.writeText(text).then(function() {
                        showNotification(copyNotification);
                    }).catch(function() {
                        // 降级方案
                        const textArea = document.createElement('textarea');
                        textArea.value = text;
                        document.body.appendChild(textArea);
                        textArea.select();
                        document.execCommand('copy');
                        document.body.removeChild(textArea);
                        showNotification(copyNotification);
                    });
                } catch (error) {
                    showError('复制失败: ' + error.message);
                }
            });
            
            // 清空按钮
            clearBtn.addEventListener('click', function() {
                if (confirm('确定要清空编辑器内容吗？')) {
                    editor.set({
                        text: '{}'
                    });
                    showSuccess('编辑器已清空！');
                }
            });
            
            // 键盘快捷键
            document.addEventListener('keydown', function(e) {
                if (e.ctrlKey || e.metaKey) {
                    switch(e.key) {
                        case 's':
                            e.preventDefault();
                            copyBtn.click();
                            break;
                        case 'f':
                            e.preventDefault();
                            formatBtn.click();
                            break;
                        case 'k':
                            e.preventDefault();
                            clearBtn.click();
                            break;
                    }
                }
            });
            
            // 初始化编辑器
            initEditor();
        });
        
        // 百度统计
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?3615c6485313357fe26e486bf42d9ec2";
          var s = document.getElementsByTagName("script")[0]; 
          s.parentNode.insertBefore(hm, s);
        })();
    </script>
</body>
</html>